package com.example.comm.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.comm.Adapter.MyPushAdapter;
import com.example.comm.Bean.Post;
import com.example.comm.Bean.User;
import com.example.comm.R;
import com.yanzhenjie.recyclerview.swipe.SwipeMenu;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItem;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItemClickListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;

import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;

public class MyPush extends AppCompatActivity {
    private SwipeMenuRecyclerView mypushrv;
    private TextView mypush_error;
    private SwipeRefreshLayout mypush_swipe;
    private ImageView back;

    private List<Post> data;
    private MyPushAdapter myPushAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mypush);
        Bmob.initialize(MyPush.this,"bf3767bf4ff13d8b86eca605eb177e0a");
        initView();
        Refresh();

        mypush_swipe.setColorSchemeResources(R.color.purple_200);
        mypush_swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Refresh();
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void Refresh() {
        //获取我发布的帖子
        User user= BmobUser.getCurrentUser(User.class);
        BmobQuery<Post> bmobQuery=new BmobQuery<>();
        bmobQuery.order("-createdAt");
        bmobQuery.addWhereEqualTo("author",user);
        bmobQuery.findObjects(new FindListener<Post>() {
            @Override
            public void done(List<Post> list, BmobException e) {
                mypush_swipe.setRefreshing(false);
                if(e==null){
                    data=list;
                    if(data.size()>0){
                        mypush_swipe.setVisibility(View.VISIBLE);

                        mypushrv.addItemDecoration(new DefaultItemDecoration(Color.BLUE));
                        mypushrv.setSwipeMenuCreator(swipeMenuCreator);
                        mypushrv.setSwipeMenuItemClickListener(swipeItemClickListener);



                        myPushAdapter=new MyPushAdapter(MyPush.this,data);
                        mypushrv.setLayoutManager(new LinearLayoutManager(MyPush.this));
                        mypushrv.setAdapter(myPushAdapter);
                    }else{ mypush_error.setVisibility(View.VISIBLE);}
                }else{
                    Toast.makeText(MyPush.this, "加载失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    SwipeMenuCreator swipeMenuCreator=new SwipeMenuCreator() {
        @Override
        public void onCreateMenu(SwipeMenu swipeLeftMenu, SwipeMenu swipeRightMenu, int viewType) {
            int width=getResources().getDimensionPixelSize(R.dimen.dp_150);
            int height= ViewGroup.LayoutParams.MATCH_PARENT;
            SwipeMenuItem deleteItem=new SwipeMenuItem(MyPush.this)
                    .setTextColor(Color.WHITE)
                    .setBackgroundColor(Color.RED)
                    .setText("删除")
                    .setWeight(width)
                    .setHeight(height);
            swipeRightMenu.addMenuItem(deleteItem);
        }
    };
    SwipeMenuItemClickListener swipeItemClickListener=new SwipeMenuItemClickListener() {
        @Override
        public void onItemClick(SwipeMenuBridge menuBridge) {
            menuBridge.closeMenu();
            int direction=menuBridge.getDirection();
            final int adapterPosition=menuBridge.getAdapterPosition();
            int position=menuBridge.getPosition();

            if(direction==SwipeMenuRecyclerView.RIGHT_DIRECTION){
                Post post=new Post();
                post.setObjectId(data.get(adapterPosition).getObjectId());
                post.delete(new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if(e==null){
                            data.remove(adapterPosition);
                            myPushAdapter.notifyDataSetChanged();
                        }else {
                            Toast.makeText(MyPush.this, "删除失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
               
            }
        }
    };

    private void initView() {
        mypushrv=findViewById(R.id.mypushrv);
        mypush_error=findViewById(R.id.mypush_error);
        mypush_swipe=findViewById(R.id.mypush_swipe);
        back=findViewById(R.id.mypush_back);
    }
}
